﻿using System.Collections.Generic;

namespace Takkou.Language
{
    // Represents a block (sequence) of statements
    public class Block : Statement
    {
        public List<Statement> stat;
        private int current;

        // block local variables and access method
        public List<Var> table = new List<Var>();
        public Var GetVar(string name)
        {
            foreach (Var v in table)
            {
                if (v.Name == name)
                    return v;
            }
            return Var.Get(name); // if it doesn't exist in the block, check as global variable
        }

        public Block()
        {
            stat = new List<Statement>();
            current = 0;
        }

        public void addStatement(Statement s)
        {
            stat.Add(s);
        }

        public void addStatement(Statement s, int num)
        {
            for (int i=0 ; i < num ; ++i)
                stat.Add(s);
        }

        public override void start()
        {
            current = 0;
        }

        public Statement getCurrent()
        {
            return stat[current];
        }

        public override bool execute()
        {
            if (current < stat.Count)
            {
                Statement cmd = stat[current];
                if (!cmd.execute())   // if command has finished
                {
                    current++;    // update reference to the next command
                }
                return true;
            }
            return false;
        }
    }
}
